package server;
import common.*;

import java.util.*;
import java.io.*;

public class ClientAccount 
{
	// TODO: hack to allow more than one client on same PC, to be removed
	protected int clientListeningPort = Constants.CLIENT_PORT;

	// These fields are stored in the file
	public String username = "";
	public String password = "";
	public ArrayList<String> blockedContacts = new ArrayList<String>();
	public ArrayList<ContactListItem> contacts = new ArrayList<ContactListItem>();
	
	// This is run-time information ONLY
	public String clientFolder = "", contactsFile = "", passwordFile = "", blockedContactsFile = "";
	public String remoteAddress = "";
	public int currentStatus = Constants.ST_ONLINE;
	// **************************************************************************** //
	public int searchContactList(String username)
	{
		for(int i=0; i<contacts.size(); ++i)
			if(contacts.get(i).username.equals(username))
				return i;
		
		return -1;
	}	
	// **************************************************************************** //
	public static boolean ClientExists(String username)
	{
		return new File(Utils.ContactsFolder+username).exists() &&
			new File(Utils.ContactsFolder+username+"/contacts.txt").exists() &&
			new File(Utils.ContactsFolder+username+"/password.txt").exists();
	}
	// **************************************************************************** //
	protected static void saveContactList(ClientAccount account)
	{
		ArrayList<String> lines = new ArrayList<String>();
		
		for(int i=0; i<account.contacts.size(); ++i)
		{
			lines.add(account.contacts.get(i).username);
			lines.add(account.contacts.get(i).group);
		} // FOR
		
		Utils.saveStringsFile(lines, account.contactsFile);
	}
	
	protected static ArrayList<ContactListItem> loadContactList(ClientAccount account)
	{
		ArrayList<String> contactList = Utils.loadStringsFile(account.contactsFile);
		ArrayList<ContactListItem> result = new ArrayList<ContactListItem>();
		
		for(int i=0; i<contactList.size(); i+=2)
		{
			ContactListItem item = new ContactListItem();
			item.username = contactList.get(i);
			item.group = contactList.get(i+1);
			
			result.add(item);
		} // FOR
	
		return result;
	}
	
	public static ClientAccount LoadContact(String username)
	{
		ClientAccount result = new ClientAccount();
		String clientFolder = Utils.ContactsFolder+username;
		
		result.username = new String(username);
		result.clientFolder = clientFolder+"/";
		result.contactsFile = Utils.ContactsFolder+username+"/contacts.txt";
		result.passwordFile = Utils.ContactsFolder+username+"/password.txt";
		result.blockedContactsFile = Utils.ContactsFolder+username+"/blockedContacts.txt";
		
		try
		{
			if(!new File(clientFolder).exists())
			{
				// This contact doesn't exist, let's create it
				new File(clientFolder).mkdir();
				new File(result.contactsFile).createNewFile();
				new File(result.passwordFile).createNewFile();
				new File(result.blockedContactsFile).createNewFile();
			} // IF
			else
			{
				result.password = Utils.loadStringFile(result.passwordFile);
				result.contacts = loadContactList(result);
				result.blockedContacts = Utils.loadStringsFile(result.blockedContactsFile);
			} // ELSE
		} // TRY
		catch(Exception e)
		{
			// All other exceptions are deadly
			System.out.println("In ClientAccount::LoadContact():");
			e.printStackTrace();
			System.exit(-1);
		} // CATCH				
		
		return result;
	}
	// **************************************************************************** //
	public void saveContact()
	{
		try
		{
			Utils.saveStringFile(this.password, this.passwordFile);
			Utils.saveStringsFile(this.blockedContacts, this.blockedContactsFile);
			saveContactList(this);// (this.contacts, this.contactsFile);
			//Utils.saveStringFile(this.group, Utils.ContactsFolder+username+"/group.txt");
		} // TRY
		catch(Exception e)
		{
			// All other exceptions are deadly
			System.out.println("In ClientAccount::saveClientAccount():");
			e.printStackTrace();
			System.exit(-1);
		} // CATCH				
	// **************************************************************************** //	
	}
}
